﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using DataControls;

namespace GiaoVien_QuanLy_DeTai
{
    public partial class LUONGCB_Form : Form
    {
        LUONGCB_Data luongcb_Data = new LUONGCB_Data();
        LUONGCB luongcb = new LUONGCB();
        GIAOVIEN_Data giaovien_Data = new GIAOVIEN_Data();
        GIAOVIEN giaovien = new GIAOVIEN();

        public LUONGCB_Form()
        {
            InitializeComponent();
        }

        private void LUONGCB_Form_Load(object sender, EventArgs e)
        {
            cbGiaovien.Items.Clear();
            giaovien_Data.giaovien_Combobox(cbGiaovien);
            txtmaLuongCB.Text = "LCB";
            txtTienLuong.Text = "";
            txtTongHSL.Text = "";
            nmHocKy.Value = 0;
            txtNamHoc.Text = DateTime.Now.Year.ToString();
        }

        private void btnHienThi_Click(object sender, EventArgs e)
        {
            luongcb.hienthi_LuongCB(dtgLuongCB);

            cbGiaovien.Items.Clear();
            giaovien_Data.giaovien_Combobox(cbGiaovien);
            txtmaLuongCB.Text = "LCB";
            txtTienLuong.Text = "";
            txtTongHSL.Text = "";
            nmHocKy.Value = 1;
            txtNamHoc.Text = DateTime.Now.Year.ToString();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtmaLuongCB.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma Luong CB");
                txtmaLuongCB.Focus();
                return;
            }
            else if (luongcb_Data.kiemtra_MaLuongCB(txtmaLuongCB.Text.ToString()))
            {
                MessageBox.Show("Ban nhap trung ma");
                txtmaLuongCB.Focus();
                return;
            }
            else if (nmHocKy.Value <= 0)
            {
                MessageBox.Show("Ban nhap sai hoc ki");
                return;
            }

            else if (cbGiaovien.Text == "")
            {
                MessageBox.Show("Ban chua chon giao vien");
                return;
            }
            else if (txtNamHoc.Text == "")
            {
                MessageBox.Show("Ban chua nhap nam hoc");
                txtNamHoc.Focus();
            }
            else
            {
                luongcb.maluongcb = txtmaLuongCB.Text.ToString();
                luongcb.magv = ((ListItem)(cbGiaovien.SelectedItem)).value;
                luongcb.tonghesoluong = (float)Convert.ToDouble(txtTongHSL.Text);
                luongcb.luongcb = (float)Convert.ToDouble(txtTienLuong.Text);
                
                try
                {
                    luongcb.hocki = (int)nmHocKy.Value;
                    luongcb.namhoc = Convert.ToInt16(txtNamHoc.Text);
                    if ((luongcb.namhoc < 1990) || ((luongcb.namhoc > DateTime.Now.Year)))
                    {
                        MessageBox.Show("Ban nhap sai nam hoc");
                        txtNamHoc.Focus();
                        return;
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Loi du lieu nam hoc" + ex.Message);
                }
                if (luongcb_Data.kiemtra_GiaoVien(luongcb))
                {
                    MessageBox.Show("Ban nhap trung thong tin");
                    return;
                }
                luongcb.them_LuongCB(luongcb);
            }

            luongcb.hienthi_LuongCB(dtgLuongCB);

            cbGiaovien.Items.Clear();
            giaovien_Data.giaovien_Combobox(cbGiaovien);
            txtmaLuongCB.Text = "LCB";
            txtTienLuong.Text = "";
            txtTongHSL.Text = "";
            nmHocKy.Value = 0;
            txtNamHoc.Text = DateTime.Now.Year.ToString();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtmaLuongCB.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma Luong CB");
                txtmaLuongCB.Focus();
                return;
            }
            else if (nmHocKy.Value <= 0)
            {
                MessageBox.Show("Ban nhap sai hoc ki");
                return;
            }

            else if (cbGiaovien.Text == "")
            {
                MessageBox.Show("Ban chua chon giao vien");
                return;
            }
            else if (txtNamHoc.Text == "")
            {
                MessageBox.Show("Ban chua nhap nam hoc");
                txtNamHoc.Focus();
            }
            else
            {
                luongcb.maluongcb = txtmaLuongCB.Text.ToString();
                luongcb.magv = ((ListItem)(cbGiaovien.SelectedItem)).value;
                luongcb.tonghesoluong = (float)Convert.ToDouble(txtTongHSL.Text);
                luongcb.luongcb = (float)Convert.ToDouble(txtTienLuong.Text);

                try
                {
                    luongcb.hocki = (int)nmHocKy.Value;
                    luongcb.namhoc = Convert.ToInt16(txtNamHoc.Text);
                    if ((luongcb.namhoc < 1990) || ((luongcb.namhoc > DateTime.Now.Year)))
                    {
                        MessageBox.Show("Ban nhap sai nam hoc");
                        txtNamHoc.Focus();
                        return;
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Loi du lieu nam hoc" + ex.Message);
                }
                if (luongcb_Data.kiemtra_GiaoVien(luongcb))
                {
                    MessageBox.Show("Ban nhap trung thong tin");
                    return;
                }
                luongcb.sua_LuongCB(luongcb);
            }

            luongcb.hienthi_LuongCB(dtgLuongCB);

            cbGiaovien.Items.Clear();
            giaovien_Data.giaovien_Combobox(cbGiaovien);
            txtmaLuongCB.Text = "LCB";
            txtTienLuong.Text = "";
            txtTongHSL.Text = "";
            nmHocKy.Value = 1;
            txtNamHoc.Text = "";
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtmaLuongCB.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma Luong CB");
                txtmaLuongCB.Focus();
                return;
            }
            else
            {
                luongcb.xoa_LuongCB(txtmaLuongCB.Text.ToString());
            }
            luongcb.hienthi_LuongCB(dtgLuongCB);

            cbGiaovien.Items.Clear();
            giaovien_Data.giaovien_Combobox(cbGiaovien);
            txtmaLuongCB.Text = "LCB";
            txtTienLuong.Text = "";
            txtTongHSL.Text = "";
            nmHocKy.Value = 1;
            txtNamHoc.Text = DateTime.Now.Year.ToString();
        }

        private void cbGiaovien_SelectedIndexChanged(object sender, EventArgs e)
        {

            int b;
            float hesohocvi = giaovien_Data.heso_hocviGiaoVien(((ListItem)(cbGiaovien.SelectedItem)).value);
            float hesochucvu = giaovien_Data.heso_chucvuGiaoVien(((ListItem)(cbGiaovien.SelectedItem)).value);
            float tonghesoluong = hesohocvi + hesochucvu;
            txtTongHSL.Text = tonghesoluong.ToString();

            int thamnien = giaovien_Data.thamnien_GiaoVien (((ListItem)(cbGiaovien.SelectedItem)).value);
            float hesoluongcanban = giaovien_Data.luong_CanBan(((ListItem)(cbGiaovien.SelectedItem)).value);
            if (DateTime.Now.Year - thamnien  >= 5)
            {
                 b = (thamnien -5)/100;
            }
            else 
                b = 0;

            float a = hesoluongcanban * tonghesoluong;
            float tongluonggiaovien = 0.0f;
            tongluonggiaovien = a + (a * b);
            double c = (double)tongluonggiaovien;

            txtTienLuong.Text = c.ToString();

            lbHocVi.Text = giaovien_Data.hocvi_GiaoVien(((ListItem)(cbGiaovien.SelectedItem)).value);
        }

        private void dtgLuongCB_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dtgLuongCB.AllowUserToAddRows = false;
            int dong = e.RowIndex;
            txtmaLuongCB.Text = dtgLuongCB.Rows[dong].Cells[0].Value.ToString();
            txtTongHSL.Text = dtgLuongCB.Rows[dong].Cells[2].Value.ToString();
            txtTienLuong.Text = dtgLuongCB.Rows[dong].Cells[3].Value.ToString();
            txtNamHoc.Text = dtgLuongCB.Rows[dong].Cells[5].Value.ToString();
            nmHocKy.Value =(int) dtgLuongCB.Rows[dong].Cells[4].Value;

            cbGiaovien.Items.Clear();
            giaovien_Data.giaovien_Combobox(cbGiaovien);

            String magiaovien = dtgLuongCB.Rows[dong].Cells[1].Value.ToString();
            luongcb_Data.layten_GiaoVien(magiaovien, cbGiaovien);
        }

      

        
 

      
    }
}
